﻿/*
color channels: r:饱和度, g:剪影程度, b:对比度
*/

Shader "DevilTeam/UI-HDR"
{
	Properties
	{
		[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
		_Color ("Tint", Color) = (1,1,1,1)

		_MaskTex ("Detail(R), Mask(A)", 2D) = "white" {}
		_DetailTex ("Detail Map", 2D) = "white" {}
		
		_StencilComp ("Stencil Comparison", Float) = 8
		_Stencil ("Stencil ID", Float) = 0
		_StencilOp ("Stencil Operation", Float) = 0
		_StencilWriteMask ("Stencil Write Mask", Float) = 255
		_StencilReadMask ("Stencil Read Mask", Float) = 255

		_ColorMask ("Color Mask", Float) = 15

		[Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0
		[Toggle(_INTENSITY_ON)] _Intensity("Intensity Mode", Float) = 0
		_IntensitySample("Intensity Sampler", Color) = (1,1,1,1)
		_IntensityShape("Intensity Shape", Float) = 1
		[Toggle(_SOFT_MASK)] _SoftMaskOn("Soft Mask On", Float) = 0
		[KeywordEnum(Off,Mul,Blend)] _Detail("Detail Type", Float) = 0

	}

	SubShader
	{
		Tags
		{ 
			"Queue"="Transparent" 
			"IgnoreProjector"="True" 
			"RenderType"="Transparent" 
			"PreviewType"="Plane"
			"CanUseSpriteAtlas"="True"
		}
		
		Stencil
		{
			Ref [_Stencil]
			Comp [_StencilComp]
			Pass [_StencilOp] 
			ReadMask [_StencilReadMask]
			WriteMask [_StencilWriteMask]
		}

		Cull Off
		Lighting Off
		ZWrite Off
		ZTest [unity_GUIZTestMode]
		Blend SrcAlpha OneMinusSrcAlpha, SrcAlpha DstAlpha
		BlendOp Add, Max
		ColorMask [_ColorMask]

		Pass
		{
			Name "Default"
		
			CGPROGRAM
			
			#pragma vertex vert
			#pragma fragment frag
			#pragma target 2.0			
			#pragma multi_compile_UNITY_UI_ALPHACLIP
			#pragma shader_feature_local _ _INTENSITY_ON
			#pragma shader_feature_local _ _SOFT_MASK
			#pragma shader_feature_local _ _DETAIL_MUL _DETAIL_BLEND

			ENDCG
		}
	}

	Fallback "UI/Default"

	CGINCLUDE
	
	#include "Packages/com.devil.gametoolkit/Shaders/CGINC/UIAdditionalData.cginc"

	#if defined(_INTENSITY_ON) || defined(_DETAIL_MUL) || defined(_DETAIL_BLEND)
		#define TEXCOORD1_ON
	#endif
	
	struct appdata_t
	{
		float4 vertex   : POSITION;
		float4 color    : COLOR;
		float4 texcoord : TEXCOORD0;
		#if defined(TEXCOORD1_ON)
			float4 texcoord1 : TEXCOORD1;
		#endif
		UNITY_VERTEX_INPUT_INSTANCE_ID
	};

	struct v2f
	{
		float4 vertex   : SV_POSITION;
		float4 color    : COLOR;
		float4 texcoord  : TEXCOORD0;
		float4 worldPosition : TEXCOORD1;
		#if defined(TEXCOORD1_ON)
			float4 texcoord1 : TEXCOORD2;
		#endif

		UNITY_VERTEX_OUTPUT_STEREO
	};

	fixed4 _Color;
	fixed4 _TextureSampleAdd;
	float4 _ClipRect;

	#define UV0(IN) IN.texcoord
	#if defined(TEXCOORD1_ON)
		#define UV1(IN) IN.texcoord1
	#else 
		#define UV1(IN) IN.texcoord
	#endif

	v2f vert(appdata_t IN)
	{
		v2f OUT;
		UNITY_SETUP_INSTANCE_ID(IN);
		UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
		OUT.worldPosition = IN.vertex;
		OUT.vertex = UnityObjectToClipPos(OUT.worldPosition);
		OUT.texcoord = IN.texcoord;
		OUT.color = _Color * IN.color;
		
		#if defined(TEXCOORD1_ON)
			OUT.texcoord1 = IN.texcoord1;
		#endif
		return OUT;
	}

	sampler2D _MainTex;

	fixed4 frag(v2f IN) : SV_TARGET
	{
		half4 color = (tex2D(_MainTex, IN.texcoord.xy) + _TextureSampleAdd) * IN.color;
		
		color.a *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect);

		Frag_AddData(color, UV0(IN), UV1(IN));

		#ifdef UNITY_UI_ALPHACLIP
			clip (color.a - 0.001);
		#endif

		return color;
	}
	ENDCG

}
